package com.settlement.system.service.csv;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.CellData;
import com.settlement.system.common.enums.CostAllocationEnum;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblQueryJoinTableService;
import com.settlement.system.service.ITblRrBillTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 订单稽核导出
 *
 * @author Ximi
 * @since 2020/10/24
 */
@Slf4j
public class TblQueryJoinTableCsvDatas extends BaseServiceCsvDatas {

    private String sqlStr;

    public TblQueryJoinTableCsvDatas(IBaseService baseService,
                                     Map<String, Object> params) {
        super(baseService, params);
        this.sqlStr = (String) params.get("sqlStr");
    }

    Map<String, Class> fieldConvertMap = new HashMap() {{
        /*put("TBL_UNIFICATION_BILL_DETAIL.SHARE_OR_NOT", ShareOrNotConvert.class);
        put("TBL_UNIFICATION_BILL_DETAIL.BEFORE_OR_AFTER_TICKET", BeforeOrAfterTicketConvert.class);
        put("TBL_UNIFICATION_BILL_DETAIL.BILL_CATEGORY", BillCategoryConvert.class);
        put("TBL_UNIFICATION_BILL_DETAIL.STATUS", UnificationStatusConvert.class);
        put("TBL_UNIFICATION_BILL_DETAIL.PROBLEM_DESC", UnificationProblemDescConvert.class);
        put("TBL_UNIFICATION_BILL_DETAIL.OUT_DATE", DateFormatConvert.class);

        put("TBL_UNIFICATION_BILL_SUMMARY.SHARE_OR_NOT", ShareOrNotConvert.class);
        put("TBL_UNIFICATION_BILL_SUMMARY.BEFORE_OR_AFTER_TICKET", BeforeOrAfterTicketConvert.class);
        put("TBL_UNIFICATION_BILL_SUMMARY.BILL_CATEGORY", BillCategoryConvert.class);*/

    }};


    @Override
    public Object beforeWrite(Object data) {

        if (data instanceof Map) {
            Map detail = (Map) data;
            convertByConvertClass(detail);
            convertTblUnificationBillDetailBillType(detail);

        }
        return data;
    }

    void convertByConvertClass(Map detail) {
        Converter converter = null;
        Object value = null;
        if (detail != null) {
            Set<String> set = fieldConvertMap.keySet();
            for (String key : set) {
                String field = key.split("\\.")[1];
                if (StringUtils.contains(sqlStr, key) && detail.containsKey(field)) {
                    try {
                        Class c = fieldConvertMap.get(key);
                        Object o = c.newInstance();
                        if (o != null) {
                            converter = (Converter) o;
                        }
                        value = detail.get(field);

                        if (converter != null && value != null) {
                            CellData cellData = converter.convertToExcelData(value, null, null);
                            value = getValue(cellData);
                            detail.put(field, value);
                        }
                    } catch (Exception ex) {
                        log.error("converter 转化异常", ex);
                        throw new RuntimeException(ex);
                    }
                }
            }
        }
    }

    private void convertTblUnificationBillDetailBillType(Map detail) {
        ITblQueryJoinTableService tblQueryJoinTableService = (ITblQueryJoinTableService) baseService;
        ITblRrBillTypeService tblRrBillTypeService = tblQueryJoinTableService.getTblRrBillTypeService();
        if (StringUtils.contains(sqlStr, "tbl_unification_bill_detail.BILL_TYPE") && detail.containsKey("BILL_TYPE")) {
            String billType = (String) detail.get("BILL_TYPE");
            if (StringUtils.isNotEmpty(billType)) {
                String nameByNo = tblRrBillTypeService.getNameByNo(billType);
                if (StringUtils.isNotEmpty(nameByNo)) {
                    billType = nameByNo;
                    detail.put("BILL_TYPE", billType);
                }
            }
        }
    }

    private void convertTblUnificationBillDetailShareOrNot(Map detail) {
        ITblQueryJoinTableService tblQueryJoinTableService = (ITblQueryJoinTableService) baseService;
        if (StringUtils.contains(sqlStr, "tbl_unification_bill_detail.SHARE_OR_NOT") && detail.containsKey("SHARE_OR_NOT")) {
            String shareOrNot = (String) detail.get("SHARE_OR_NOT");
            if (StringUtils.isNotEmpty(shareOrNot)) {
                String shareOrNotStr = shareOrNot.replace(CostAllocationEnum.ShareOrNotEnum.NO.getNo(), CostAllocationEnum.ShareOrNotEnum.NO.getName())
                        .replace(CostAllocationEnum.ShareOrNotEnum.YES.getNo(), CostAllocationEnum.ShareOrNotEnum.YES.getName());
                detail.put("SHARE_OR_NOT", shareOrNotStr);
            }
        }
    }

    private void convertTblUnificationBillDetailBeforeOrAfterTicket(Map detail) {
        ITblQueryJoinTableService tblQueryJoinTableService = (ITblQueryJoinTableService) baseService;
        if (StringUtils.contains(sqlStr, "tbl_unification_bill_detail.BEFORE_OR_AFTER_TICKET") && detail.containsKey("BEFORE_OR_AFTER_TICKET")) {
            String beforeOrAfterTicket = (String) detail.get("BEFORE_OR_AFTER_TICKET");
            if (StringUtils.isNotEmpty(beforeOrAfterTicket)) {
                String beforeOrAfterTicketStr = beforeOrAfterTicket
                        .replace(CostAllocationEnum.BeforeOrAfterTicketEnum.AFTER.getNo(), CostAllocationEnum.BeforeOrAfterTicketEnum.AFTER.getName())
                        .replace(CostAllocationEnum.BeforeOrAfterTicketEnum.BEFORE.getNo(), CostAllocationEnum.BeforeOrAfterTicketEnum.BEFORE.getName())
                        .replace(CostAllocationEnum.BeforeOrAfterTicketEnum.NOT_INVOLVE.getNo(), CostAllocationEnum.BeforeOrAfterTicketEnum.NOT_INVOLVE.getName());
                detail.put("BEFORE_OR_AFTER_TICKET", beforeOrAfterTicketStr);
            }
        }
    }

    private Object getValue(CellData cellData) {

        if (cellData == null || cellData.getType() == null) {
            return null;
        }
        if (cellData.getType().equals(CellDataTypeEnum.STRING)) {
            return cellData.getStringValue();
        }
        return cellData.getStringValue();
    }
}
